#include "adc.h"

struct ADC_DATA RF_Data = {
    .v48_a = 1,
    .jb_a = 1
};

void ADC_DMA_Init(void)
{
    //开启ADC1 DMA1 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    //设置ADC分频系数 12MHz
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
    //配置ADC采样专用的引脚模式 GPIO_Mode_AIN
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = AD48V1_PIN|AD48V2_PIN|C1_JB1_PIN|C1_JB2_PIN|C2_JB1_PIN|C2_JB2_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //配置ADC1多通道和采样时间
    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 2, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 5, ADC_SampleTime_55Cycles5);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 6, ADC_SampleTime_55Cycles5);
    
    ADC_InitTypeDef ADC_InitStructure;
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;      //单独转换
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  //数据右对齐
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;      //使能连续模式
	ADC_InitStructure.ADC_ScanConvMode = ENABLE;            //使能扫描模式
	ADC_InitStructure.ADC_NbrOfChannel = 6;                 //通道数
	ADC_Init(ADC1, &ADC_InitStructure);
    
    DMA_InitTypeDef DMA_InitStructure;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;             //ADC数据寄存器地址
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据大小
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;            //地址禁止自增
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&RF_Data;                 //存储器地址
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;         //数据大小
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                     //允许地址自增
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                          //设置源对象
	DMA_InitStructure.DMA_BufferSize = 6;                                       //传输次数
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                             //循环传输
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                                //禁止软件触发
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;                       //优先级
	DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    
    DMA_Cmd(DMA1_Channel1, ENABLE);     //使能DMA通道1
    ADC_DMACmd(ADC1, ENABLE);           //使能ADC1的DMA请求
    ADC_Cmd(ADC1, ENABLE);              //使能ADC1
    //开始校准ADC
    ADC_ResetCalibration(ADC1);
	while (ADC_GetResetCalibrationStatus(ADC1) == SET);
	ADC_StartCalibration(ADC1);
	while (ADC_GetCalibrationStatus(ADC1) == SET);
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);     //开始ADC1转换
    
    //使能定时器4
    Timer_Init(TIM4, 1000, 7200);   //100ms
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&NVIC_InitStructure);
    TIM_Cmd(TIM4, ENABLE);
}
